#include <iostream>
using namespace std;
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string>
#include <assert.h>
#include <string.h>

int main()
{
    int fds[2];
    int n = pipe(fds);
    assert(n == 0);

    pid_t id = fork();
    if (id == 0)
    {
        close(fds[0]);
        const char *arr = "我是子进程";
        int cnt = 10;
        while (cnt--)
        {
            char buffer[128];
            snprintf(buffer, sizeof(buffer), "%s:%d", arr, cnt);
            write(fds[1], buffer, strlen(buffer));
            sleep(1);
        }
    }
    close(fds[1]);
    while (1)
    {
        char buffer[128];
        ssize_t n = read(fds[0], buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
             cout << "#:" << buffer << endl;
        }else{
            break;
        }
       
    }
    int ret = waitpid(id, nullptr, 0);
    assert(ret == id);

    return 0;
}